import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const router=new VueRouter({
  routes: [
    {
      path:'/', 
      redirect:'/login'
    },
    {
      path: '/login', 
      name: 'login',
      component:()=>import('./components/login.vue')
    },
    {path: '/home',
    name: 'home',
    component:()=>import('./views/Home.vue'),
    redirect:'/users',
      children:[
  
       { path: '/users',
        name: 'users',
        component:()=>import('./views/users.vue')
       },
       { path: '/rights',
        name: 'rights',
        component:()=>import('./views/auth/authList.vue')
       },
       {
         path:'/roles', 
         name:'roles', 
         component: ()=>import('./views/auth/roles.vue')
       }
  ]
  }
  ]
})

//有token放行，没token，跳转login
router.beforeEach((to, from, next) => {
  //to and from are Route Object,next() must be called to resolve the hook}
  if(to.path==='/login'){
    next();
  }else{
    const token=window.sessionStorage.getItem('token');
    if(token){
      next();
    }else{
      next('/login');
    }
  }
})
export default router;